package com.ruoyi.competitionNEW.service.impl;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.competitionNEW.domain.Competition;
import com.ruoyi.competitionNEW.mapper.CompetitionMapper;
import com.ruoyi.competitionNEW.service.ICompetitionService;
import com.ruoyi.file_info.domain.CompetitionFileInfo;
import com.ruoyi.file_info.mapper.CompetitionFileInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static org.apache.commons.math3.random.RandomGeneratorFactory.convertToLong;


/**
 * 赛事Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-21
 */
@Service
public class CompetitionServiceImpl implements ICompetitionService
{
    @Autowired
    private CompetitionMapper competitionMapper;
    @Autowired
    private CompetitionFileInfoMapper competitionFileInfoMapper;

    /**
     * 查询赛事
     * 
     * @param id 赛事主键
     * @return 赛事
     */
    @Override
    public Competition selectCompetitionById(Long id)
    {
        return competitionMapper.selectCompetitionById(id);
    }

    /**
     * 查询赛事列表
     * 
     * @param competition 赛事
     * @return 赛事
     */
    @Override
    public List<Competition> selectCompetitionList(Competition competition)
    {
        return competitionMapper.selectCompetitionList(competition);
    }

    /**
     * 新增赛事
     * 
     * @param competition 赛事
     * @return 结果
     */
    @Override
    public int insertCompetition(Competition competition)
    {
        competition.setCreateTime(DateUtils.getNowDate());
        competition.setUserId(SecurityUtils.getUsername());
        return competitionMapper.insertCompetition(competition);
    }

    /**
     * 修改赛事
     * 
     * @param competition 赛事
     * @return 结果
     */
    @Override
    public int updateCompetition(Competition competition)
    {
        competition.setUpdateTime(DateUtils.getNowDate());
        competition.setUpdateuserId(SecurityUtils.getUsername());
        return competitionMapper.updateCompetition(competition);
    }

    /**
     * 批量删除赛事
     * 
     * @param ids 需要删除的赛事主键
     * @return 结果
     */
    @Override
    public int deleteCompetitionByIds(Long[] ids)
    {
        return competitionMapper.deleteCompetitionByIds(ids);
    }

    /**
     * 删除赛事信息
     * 
     * @param id 赛事主键
     * @return 结果
     */
    @Override
    public int deleteCompetitionById(Long id)
    {
        return competitionMapper.deleteCompetitionById(id);
    }



    /**
     * 试题管理
     * @param competitionFileInfo
     * @return
     */
    @Override
    public List<CompetitionFileInfo> selectCompetitionFileInfoList(CompetitionFileInfo competitionFileInfo) {
        competitionFileInfo.setFileType("试题附件");
        return competitionMapper.selectCompetitionFileInfoList(competitionFileInfo);
    }

    @Override
    public void addExamQuestions(Map map) {
        CompetitionFileInfo competitionFileInfo = new CompetitionFileInfo();
        /**
         * 更具competitionId查出赛事名称,方便存储
         * competitionId是Object类型,将其转换为Long类型
         *
         * Object competitionId = map.get("competitionId");
         * Long longValue = Long.parseLong(competitionId.toString());
         * Competition competition = competitionMapper.selectCompetitionById(longValue);
         */

        //更具id查询赛事名称
        Competition competition = competitionMapper.selectCompetitionById(Long.parseLong(map.get("competitionId").toString()));
        //转换
        competitionFileInfo.setFileType("试题附件");
        competitionFileInfo.setFileId(String.valueOf(UUID.randomUUID()));
        competitionFileInfo.setFileName(competition.getName() + "@" +String.valueOf(map.get("name")));
        competitionFileInfo.setFilePath(String.valueOf(map.get("url")));
        competitionFileInfo.setCreatetime(DateUtils.getNowDate());
        competitionFileInfo.setCreateby(SecurityUtils.getUsername());
        competitionFileInfoMapper.insertCompetitionFileInfo(competitionFileInfo);
    }
}
